package com.haierp.controller;

import java.util.Date;
import java.util.List;
import java.util.Set;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.haierp.exception.ErpCommonException;
import com.haierp.mapper.SellerMapper;
import com.haierp.model.Seller;
import com.haierp.service.ISellerService;
import com.haierp.util.JsonPageResult;
import com.haierp.util.JsonResult;
import com.haierp.vo.SellerQueryVO;
import com.haierp.vo.SellerTypeQueryVO;
import com.wangzhixuan.commons.base.BaseController;
import com.wangzhixuan.commons.shiro.ShiroUser;

/**
 * 
 * Title: SellerController.java
 * Description: Seller Controller
 *
 * @author jc
 * Mar 18, 2017
 *
 */

@Service("sellerController")
@Controller
@RequestMapping("/seller")
public class SellerController extends BaseController {

	@Autowired
	private ISellerService iSellerService;
	@Autowired
	private SellerMapper sellerMapper;	
	
	@RequestMapping("/add")
	@ResponseBody
	public Object add(Seller seller) {
		JsonResult<String> result = new JsonResult<>();
		ShiroUser one = this.getShiroUser();
		if (null == seller.getId()) {
			SellerQueryVO sellerQueryVO = new SellerQueryVO();
			sellerQueryVO.setCode(seller.getCode());
			//Check code is unique
			Integer count = iSellerService.querySellersCount(sellerQueryVO);
			if (count > 0) {
				result.buildMsg("经销商代码不可以重复").buildIsSuccess(false);
			} else {
				seller.setGmtCreate(new Date());
				seller.setGmtModify(new Date());
				seller.setCompanyId(one.getCompanyId());
				result.buildIsSuccess(iSellerService.insert(seller));
			}
		} else {
			result.buildMsg("错误数据").buildIsSuccess(false);
		}
		
		return result;
	}
	
	@RequestMapping("/update")
	@ResponseBody
	public Object update(Seller seller) {
		JsonResult<String> result = new JsonResult<>();
		
		if (null != seller.getId()) {
			Seller tjSeller = new Seller();
			tjSeller.setCode(seller.getCode());
			Seller selSeller = iSellerService.selectOne(tjSeller);
			if(selSeller!=null && selSeller.getId()!=seller.getId()) {
				return result.buildMsg("经销商代码不可以重复").buildIsSuccess(false);
			}
			
			
			seller.setGmtCreate(null);
			seller.setGmtModify(new Date());
			result.buildIsSuccess(iSellerService.updateSelectiveById(seller));
		} else {
			result.buildMsg("错误数据").buildIsSuccess(false);
		}
		
		return result;
	}

	@RequestMapping("/delete")
	@ResponseBody
	public Object delete(Seller seller) {
		JsonResult<String> result = new JsonResult<>();

		if (null != seller.getId()) {
			Integer num = iSellerService.querySaleCountInOrder(seller.getId());
			if(num!=null && num>0) {
				result.buildMsg("此销售人员已产生订单，不能删除！").buildIsSuccess(false);
			} else {
				result.buildIsSuccess(iSellerService.deleteSelective(seller));
			}
		} else {
			result.buildMsg("错误数据").buildIsSuccess(false);
		}
		
		return result;
	}
	
	@RequestMapping("/query")
	@ResponseBody
	public Object query(Long id) {
		JsonResult<Seller> result = new JsonResult<>();
		
		return result.buildData(iSellerService.selectById(id)).buildIsSuccess(true);
	}
	
	@RequestMapping("/querySellerList")
	@ResponseBody
	public Object querySellerList(SellerQueryVO sellerQueryVO) {
		//如果是代理
		ShiroUser shiroUser = this.getShiroUser();
		Set<String> roles = shiroUser.getRoles();
		if(roles.contains("irhdaili")) {
			String[] logingNameArr = shiroUser.getLoginName().split("_");
			if(logingNameArr.length<2 || StringUtils.isBlank(logingNameArr[1])) {
				throw new ErpCommonException("用户权限异常");
			}
			sellerQueryVO.setId(Integer.parseInt(logingNameArr[1]));
		}
		JsonPageResult<List<Seller>> result = iSellerService.querySellerList(sellerQueryVO);
		
		return result.buildIsSuccess(true);
	}
	
	@RequestMapping("/querySellerListAll")
	@ResponseBody
	public Object querySellerListAll() {
		JsonPageResult<List<Seller>> sellerResult = new JsonPageResult<>();
		List<Seller> sellers = sellerMapper.querySellersAll();
		sellerResult.setData(sellers);
		return sellerResult.buildIsSuccess(true);
	}
}
